package com.stylefeng.guns.rest.modular.film.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.film.FilmServiceApi;
import com.stylefeng.guns.api.film.vo.*;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class DefaultFilmServiceImpl implements FilmServiceApi {

    @Autowired
    private MoocFilmTMapper moocFilmTMapper;

    @Autowired
    private MoocBannerTMapper moocBannerTMapper;

    @Autowired
    private MoocCatDictTMapper moocCatDictTMapper;

    @Autowired
    private MoocYearDictTMapper moocYearDictTMapper;

    @Autowired
    private MoocSourceDictTMapper moocSourceDictTMapper;

    @Autowired
    private MoocActorTMapper moocActorTMapper;

    @Autowired
    private MoocFilmInfoTMapper moocFilmInfoTMapper;

    @Autowired
    private MoocFilmActorTMapper moocFilmActorTMapper;

    @Override
    public List<BannerVO> getBanners() {
        List<BannerVO> result = new ArrayList<>();
        List<MoocBannerT> moocBanners = moocBannerTMapper.selectList(null);
        for (MoocBannerT moocBannerT:
             moocBanners) {
            BannerVO bannerVO = new BannerVO();
            bannerVO.setBannerAddress(moocBannerT.getBannerAddress());
            bannerVO.setBannerId(moocBannerT.getUuid() + "");
            bannerVO.setBannerUrl(moocBannerT.getBannerUrl());

            result.add(bannerVO);
        }
        return result;
    }

    private List<FilmInfo> getFilmInfos(List<MoocFilmT> moocFilms){
        List<FilmInfo> filmInfos = new ArrayList<>();
        for (MoocFilmT moocfilmT :
                moocFilms) {
            FilmInfo filmInfo = new FilmInfo();
            filmInfo.setShowTime(DateUtil.getDay(moocfilmT.getFilmTime()));
            filmInfo.setScore(moocfilmT.getFilmScore());
            filmInfo.setImgAddress(moocfilmT.getImgAddress());
            filmInfo.setFilmType(moocfilmT.getFilmType());
            filmInfo.setFilmScore(moocfilmT.getFilmScore());
            filmInfo.setFilmName(moocfilmT.getFilmName());
            filmInfo.setFilmId(moocfilmT.getUuid() + "");
            filmInfo.setExpectNum(moocfilmT.getFilmPresalenum());
            filmInfo.setBoxNum(moocfilmT.getFilmBoxOffice());

            filmInfos.add(filmInfo);
        }

        return filmInfos;
    }

    @Override
    public FilmVO getHotFilms(boolean isLimit, int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        FilmVO filmVO = new FilmVO();
        List<FilmInfo> filmInfos = new ArrayList<>();
        //热门影片的限制条件
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", 1);
        if (isLimit){
            Page<MoocFilmT> page = new Page<>(1, nums);
            List<MoocFilmT> moocFilms = moocFilmTMapper.selectPage(page, entityWrapper);
            filmInfos = getFilmInfos(moocFilms);
            filmVO.setFilmNum(filmInfos.size());
            filmVO.setFilmInfo(filmInfos);

        }else{
            Page page = null;
            switch (sortId){
                case 1:
                    page = new Page(nowPage, nums, "film_box_office");
                    break;
                case 2:
                    page = new Page(nowPage, nums, "film_time");
                    break;
                case 3:
                    page = new Page(nowPage, nums, "film_score");
                    break;
                default:
                    page = new Page(nowPage, nums, "film_box_office");
                    break;
            }
            if (sourceId != 99){
                entityWrapper.eq("film_source", sourceId);
            }
            if (yearId != 99){
                entityWrapper.eq("film_date", yearId);
            }
            if (catId != 99){
                String catStr = "%#" + catId + "#%";
                entityWrapper.like("film_cats", catStr);
            }

            List<MoocFilmT> moocFilms = moocFilmTMapper.selectPage(page, entityWrapper);
            filmInfos = getFilmInfos(moocFilms);
            filmVO.setFilmNum(filmInfos.size());
            filmVO.setFilmInfo(filmInfos);

            int totalCounts = moocFilmTMapper.selectCount(entityWrapper);
            int totalPages = totalCounts / nums + 1;

            filmVO.setTotalPage(totalPages);
            filmVO.setNowPage(nowPage);

        }
        return filmVO;
    }

    @Override
    public FilmVO getSoonFilms(boolean isLimit, int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        FilmVO filmVO = new FilmVO();
        List<FilmInfo> filmInfos = new ArrayList<>();
        //即将上映影片的限制条件
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", 2);
        if (isLimit){
            Page page = new Page(1, nums);
            List<MoocFilmT> moocFilms = moocFilmTMapper.selectPage(page, entityWrapper);
            filmInfos = getFilmInfos(moocFilms);
            filmVO.setFilmNum(filmInfos.size());
            filmVO.setFilmInfo(filmInfos);

        }else{
            Page page = null;
            switch (sortId){
                case 1:
                    page = new Page(nowPage, nums, "film_preSaleNum");
                    break;
                case 2:
                    page = new Page(nowPage, nums, "film_time");
                    break;
                case 3:
                    page = new Page(nowPage, nums, "film_preSaleNum");
                    break;
                default:
                    page = new Page(nowPage, nums, "film_preSaleNum");
                    break;
            }
            if (sourceId != 99){
                entityWrapper.eq("film_source", sourceId);
            }
            if (yearId != 99){
                entityWrapper.eq("film_date", yearId);
            }
            if (catId != 99){
                String catStr = "%#" + catId + "#%";
                entityWrapper.like("film_cats", catStr);
            }

            List<MoocFilmT> moocFilms = moocFilmTMapper.selectPage(page, entityWrapper);
            filmInfos = getFilmInfos(moocFilms);
            filmVO.setFilmNum(filmInfos.size());
            filmVO.setFilmInfo(filmInfos);

            int totalCounts = moocFilmTMapper.selectCount(entityWrapper);
            int totalPages = totalCounts / nums + 1;

            filmVO.setTotalPage(totalPages);
            filmVO.setNowPage(nowPage);
        }
        return filmVO;
    }

    @Override
    public FilmVO getClassicFilms(int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        FilmVO filmVO = new FilmVO();
        List<FilmInfo> filmInfos = new ArrayList<>();
        //即将上映影片的限制条件
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", 3);
        Page page = null;
        switch (sortId){
            case 1:
                page = new Page(nowPage, nums, "film_box_office");
                break;
            case 2:
                page = new Page(nowPage, nums, "film_time");
                break;
            case 3:
                page = new Page(nowPage, nums, "film_score");
                break;
            default:
                page = new Page(nowPage, nums, "film_box_office");
                break;
        }
        if (sourceId != 99){
            entityWrapper.eq("film_source", sourceId);
        }
        if (yearId != 99){
            entityWrapper.eq("film_date", yearId);
        }
        if (catId != 99){
            String catStr = "%#" + catId + "#%";
            entityWrapper.like("film_cats", catStr);
        }

        List<MoocFilmT> moocFilms = moocFilmTMapper.selectPage(page, entityWrapper);
        filmInfos = getFilmInfos(moocFilms);
        filmVO.setFilmNum(filmInfos.size());
        filmVO.setFilmInfo(filmInfos);

        int totalCounts = moocFilmTMapper.selectCount(entityWrapper);
        int totalPages = totalCounts / nums + 1;

        filmVO.setTotalPage(totalPages);
        filmVO.setNowPage(nowPage);

        return filmVO;
    }

    @Override
    public List<FilmInfo> getBoxRanking() {
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", 1);
        Page page = new Page(1, 10, "film_box_office");
        List<MoocFilmT> moocFilms = moocFilmTMapper.selectPage(page, entityWrapper);
        List<FilmInfo> filmInfos = getFilmInfos(moocFilms);

        return filmInfos;
    }

    @Override
    public List<FilmInfo> getExpectRanking() {
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", 2);
        Page page = new Page(1, 10, "film_preSaleNum");
        List<MoocFilmT> moocFilms = moocFilmTMapper.selectPage(page, entityWrapper);
        List<FilmInfo> filmInfos = getFilmInfos(moocFilms);

        return filmInfos;
    }

    @Override
    public List<FilmInfo> getTop() {
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", 1);
        Page page = new Page(1, 10, "film_source");
        List<MoocFilmT> moocFilms = moocFilmTMapper.selectPage(page, entityWrapper);
        List<FilmInfo> filmInfos = getFilmInfos(moocFilms);

        return filmInfos;
    }

    @Override
    public List<CatVO> getCats() {
        List<CatVO> cats = new ArrayList<>();
        List<MoocCatDictT> moocCats = moocCatDictTMapper.selectList(null);
        for (MoocCatDictT moocCatDicT :
                moocCats) {
            CatVO catVO = new CatVO();
            catVO.setCatId(moocCatDicT.getUuid() + "");
            catVO.setCatName(moocCatDicT.getShowName());

            cats.add(catVO);
        }
        return cats;
    }

    @Override
    public List<SourceVO> getSources() {
        List<MoocSourceDictT> moocSources = moocSourceDictTMapper.selectList(null);
        List<SourceVO> sources = new ArrayList<>();
        for (MoocSourceDictT moocSourceDicT :
                moocSources) {
            SourceVO sourceVO = new SourceVO();
            sourceVO.setSourceId(moocSourceDicT.getUuid() + "");
            sourceVO.setSourceName(moocSourceDicT.getShowName());

            sources.add(sourceVO);
        }
        return sources;
    }

    @Override
    public List<YearVO> getYears() {
        List<YearVO> years = new ArrayList<>();
        List<MoocYearDictT> moocYears = moocYearDictTMapper.selectList(null);
        for (MoocYearDictT moocoocYearDictT :
                moocYears) {
            YearVO yearVO = new YearVO();
            yearVO.setYearId(moocoocYearDictT.getUuid() + "");
            yearVO.setYearName(moocoocYearDictT.getShowName());

            years.add(yearVO);
        }
        return years;
    }

    @Override
    public FilmDetailVO getFilmDetail(int searchType, String searchParam) {
        FilmDetailVO filmDetail = new FilmDetailVO();
        if (searchType == 1){
            filmDetail = moocFilmTMapper.getFilmDetailByName("%" + searchParam + "%");
        }else{
            filmDetail = moocFilmTMapper.getFilmDetailById(searchParam);
        }

        return filmDetail;
    }

    private MoocFilmInfoT getFilmInfo(String filmId){
        MoocFilmInfoT moocFilmInfo = new MoocFilmInfoT();
        moocFilmInfo.setFilmId(filmId);
        MoocFilmInfoT moocFilmInfoT = moocFilmInfoTMapper.selectOne(moocFilmInfo);

        return moocFilmInfoT;
    }

    @Override
    public FilmDescVO getFilmDesc(String filmId) {
        MoocFilmInfoT filmInfo = getFilmInfo(filmId);
        FilmDescVO filmDesc = new FilmDescVO();
        filmDesc.setBiography(filmInfo.getBiography());
        filmDesc.setFilmId(filmInfo.getFilmId());

        return filmDesc;
    }

    @Override
    public ImgVO getImgs(String filmId) {
        MoocFilmInfoT filmInfo = getFilmInfo(filmId);
        String imgStr = filmInfo.getFilmImgs();
        String[] imgs = imgStr.split(",");
        ImgVO img = new ImgVO();
        img.setMainImg(imgs[0]);
        img.setImg01(imgs[1]);
        img.setImg02(imgs[2]);
        img.setImg03(imgs[3]);
        img.setImg04(imgs[4]);

        return img;
    }

    @Override
    public ActorVO getDirector(String filmId) {
        MoocFilmInfoT filmInfo = getFilmInfo(filmId);
        ActorVO director = new ActorVO();
        Integer directorId = filmInfo.getDirectorId();
        MoocActorT moocActorT = moocActorTMapper.selectById(directorId);
        director.setDirectorName(moocActorT.getActorName());
        director.setImgAddress(moocActorT.getActorImg());


        return director;
    }

    @Override
    public  List<ActorVO> getActors(String filmId) {

        List<ActorVO> actors = moocFilmActorTMapper.getActors(filmId);

        return actors;
    }
}
